package com.css.fxfzypg.modules.actionTrack.service.impl;

import com.css.fxfzypg.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzypg.modules.actionTrack.repository.ActionTrackRepository;
import com.css.fxfzypg.modules.actionTrack.service.ActionTrackService;
import com.css.fxfzypg.util.GeoUtil;
import com.css.fxfzypg.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/27 10:17
 **/
@Service
public class ActionTrackServiceImpl implements ActionTrackService {
    @Autowired
    ActionTrackRepository actionTrackRepository;

    @Override
    public String save(ActionTrackEntity actionTrackEntity) throws Exception{
        String id = UUIDGenerator.getUUID();
        actionTrackEntity.setCreateTime(new Date());
        actionTrackEntity.setFootId(id);
        if(!"0".equals(actionTrackEntity.getPhasePoint())){
            ActionTrackEntity lastActionTrack=actionTrackRepository.fingByFootId(actionTrackEntity.getLastFootId());
            double lastLongitude = Double.parseDouble(lastActionTrack.getLongitude());
            double lastLatitude = Double.parseDouble(lastActionTrack.getLatitude());
            double longitude = Double.parseDouble(actionTrackEntity.getLongitude());
            double latitude = Double.parseDouble(actionTrackEntity.getLatitude());
            Double distance = GeoUtil.GetDistance(longitude, latitude, lastLongitude, lastLatitude);
            if(Double.doubleToLongBits(distance) >= Double.doubleToLongBits(50.0)) {
                actionTrackRepository.save(actionTrackEntity);
            }else{
                throw new RuntimeException("当前轨迹点距离上一个轨迹点小于50米，保存失败");
            }
        }else{
            actionTrackRepository.save(actionTrackEntity);
        }
        return id;
    }

    @Override
    public List<ActionTrackEntity> getActionTracks(String userId,String startTime,String endTime) {
        List<ActionTrackEntity> ActionTrackS=actionTrackRepository.getActionTracks( userId, startTime, endTime);
        return ActionTrackS;
    }
}
